package net.amsoft.iservice.management.webapp.struts.form;

import javax.servlet.http.HttpServletRequest;

import net.amsoft.iservice.management.util.IBrokerConstants;

import org.apache.log4j.Logger;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;

/**
* @author Mukta Keshav
* @Web http://www.amsoft.net
* @Email mukta@amsoft.net
*/

/**
 * Form bean for Related-News Image File Upload.
 *
*/
public class RelatedNewsUploadForm extends ActionForm
{
    private static Logger logger = Logger.getLogger(RelatedNewsUploadForm.class);
    private FormFile theFile;
    private String action;

  /**
   * @return Returns the theFile.
   */
    public FormFile getTheFile() {
        logger.info("getTheFile(): Returning theFile >> " + theFile);
        return theFile;
    }
    
  /**
   * @param theFile The FormFile to set.
   */
    public void setTheFile(FormFile theFile) {
        logger.info("setTheFile(): Setting theFile >> " + theFile);
        this.theFile = theFile;
    }
  
    public String getAction() {
        logger.info("getAction(): Returning action >> " + action);
        return action;
    }

    public void setAction(String action) {
        logger.info("setAction(): Setting action >> " + action);
        this.action = action;
    }

    /**
     * Checks if user has entered an invalid file format
     */
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
	{
		// Check for mandatory data
		ActionErrors errors = new ActionErrors();

		// Check if there is any input
		if ("upload".equals(action))
		{
			String error = checkEmpty();
		    if (error.length()>0)
		        errors.add("theFile", new ActionMessage(error));
		    else
		    {
		        // Check if input is valid
		        error = checkInvalid();
		        if (error.length()>0)
		            errors.add("theFile", new ActionMessage(error));
		    }
		}
	        
		return errors;
	}
	
	/**
	 * Checks if input is null
	 * @return error key
	 */
	private String checkEmpty()
	{
	    String error = "";
		if ((theFile==null)||(theFile.equals(""))||(theFile.getFileSize()<1))
		{
		    logger.error("checkEmpty(): Found null input");		 
			error = IBrokerConstants.IMAGE_EMPTY;
		}
		return error;
	}

	/**
	 * Checks if input is valid
	 * @return error key
	 */
	private String checkInvalid()
	{
	    String error = "";
	    
	    // Get valid image file formats from Resource Bundle
	    String validFormats = IBrokerConstants.getProperty(IBrokerConstants.RELATEDNEWS_IMAGE_FORMATS);
	    logger.info("checkInvalid(): Valid File Formats >> " + validFormats);		 
	    
	    // Check if valid formats are given
	    if (validFormats != null)
	    {
	        validFormats.toLowerCase();
		    String sFileName = theFile.getFileName().toLowerCase();
		    boolean valid = false;

		    // Check if one or more valid formats are given
	        if (validFormats.indexOf(',') > -1)
		    {
		        String array[] = validFormats.split(",");
			    int i=0;
			    while ((!(valid)) && i<array.length)
			    {
			        logger.info("checkInvalid(): Checking if " + sFileName + " endsWith " + array[i]);
			        valid = (sFileName.endsWith(array[i]));
			        i++;
			    }
		    }
	        else
	        {
	            valid = (sFileName.endsWith(validFormats));
	        }
		    
	        // Check if provided file format is valid
	        if (!(valid))
			{
			    logger.error("checkInvalid(): Found Invalid File Format");		 
				error = IBrokerConstants.IMAGE_INVALID;
			}
	    }
	    
	    return error;
	}
	
}